package com.beiming.kotlindemo.activity

import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.beiming.kotlindemo.R
import kotlinx.android.synthetic.main.activity_second.*

class SecondActivity : BaseActivity() {

    /**
     * 伴生类（每个类只能有一个伴生类对象） 静态方法定义（非真正的静态方法）
     */
    companion object {
        /**
         * @JvmStatic静态方法
         * @JvmStatic注解只能加在单例类和companion object的方法上
         */
        @JvmStatic
        fun actionStart(context: Context, data1: String, data2: String) {
            val intent = Intent(context, SecondActivity::class.java).apply {
                putExtra("params1", data1)
                putExtra("params2", data2)
            }
//            intent.putExtra("params1", data1)
//            intent.putExtra("params2", data2)
            context.startActivity(intent)
        }
    }

    override fun getContentResId() = R.layout.activity_second

    override fun initView() {
        btn2.setOnClickListener {
            Toast.makeText(this, "You clicked Button 2", Toast.LENGTH_SHORT).show()
            setReturnData()
            finish()
        }
    }

    override fun initData() {
        val extraData = intent.getStringExtra("extra_data");
        Log.d(TAG, "extra data is $extraData")
    }

    /**
     * 设置返回数据
     */
    private fun setReturnData() {
        val intent = Intent()
        intent.putExtra("data_return", "Hello FirstActivity")
        setResult(2, intent)
    }

    override fun onBackPressed() {
        setReturnData()
        finish()
    }
}
